package study.字符串耗时操作;

public class Snippet {

 public static void main(String[] args) {
//	 String s = "";
//		long sBeginTime = System.currentTimeMillis();
//		for (int i = 0; i < 100000; i++) {
//		    s+="s";
//		}
//		long sEndTime = System.currentTimeMillis();
//		System.out.println("s拼接100000遍s耗时: " + (sEndTime - sBeginTime) + "ms");
//		
//		StringBuffer s1 = new StringBuffer();
//		long s1BeginTime = System.currentTimeMillis();
//		for (int i = 0; i < 100000; i++) {
//		    s1.append("s");
//		}
//		long s1EndTime = System.currentTimeMillis();
//		System.out.println("s1拼接100000遍s耗时: " + (s1EndTime - s1BeginTime) + "ms");
//		
//		StringBuilder s2 = new StringBuilder();
//		long s2BeginTime = System.currentTimeMillis();
//		for (int i = 0; i < 100000; i++) {
//		    s2.append("s");
//		}
//		long s2EndTime = System.currentTimeMillis();
//		System.out.println("s2拼接100000遍s耗时: " + (s2EndTime - s2BeginTime) + "ms");
//		
//		
		
		
		
		
		
		
		
		
		
		
		Integer num  = 0;
		int loop = 100000;  // 将结果放大100000倍，以便于观察结果
		long beginTime = System.currentTimeMillis();
		for (int i = 0; i < loop; i++) {
		    String s = num+"";
		}
		long endTime = System.currentTimeMillis();
		System.out.println("+\"\"的方式耗时: " + (endTime - beginTime) + "ms");


		beginTime = System.currentTimeMillis();
		for (int i = 0; i < loop; i++) {
		    String s = String.valueOf(num);
		}
		endTime = System.currentTimeMillis();
		System.out.println("String.valueOf()的方式耗时: " + (endTime - beginTime) + "ms");

		beginTime = System.currentTimeMillis();
		for (int i = 0; i < loop; i++) {
		    String s = num.toString();
		}
		endTime = System.currentTimeMillis();
		System.out.println("toString()的方式耗时: " + (endTime - beginTime) + "ms");
}	
	
}

